package com.tkjt.pms.impt.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import com.tkjt.pms.impt.NewKpiIndexImporterService;
import com.tkjt.pms.impt.common.KpiConsts;
import com.tkjt.pms.impt.vos.KpiIndexDataVo;
import com.tkjt.pms.impt.vos.KpiIndexValueVo;
import com.tkjt.pms.impt.vos.NewCIndexVo;
import com.tkjt.pms.impt.vos.NewGIndexVo;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.modules.common.service.TkIndexInfoService;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

@Slf4j
@Service
public class NewIndexImporter extends ServiceImpl<TkIndexValueMapper, TkIndexValue> implements NewKpiIndexImporterService {


	@Autowired
	private NewKpiImportUpdater kpiImportUpdater;

    @Override
	public void doCImporter(String filePath,Set<String> noCreate,Set<String> doubleIndexValueCodes){

		ImportParams params = new ImportParams();
		params.setTitleRows(1);
		params.setHeadRows(2);
		params.setNeedSave(false);

		File file = new File(filePath);

		List<NewCIndexVo> results = ExcelImportUtil.importExcel(file, NewCIndexVo.class, params);
		log.info("results size=" + results.size());

		/**************************************************************************
		 * 处理权重  处理指标值
		 */
		try {
			this.processCIndexData(results,noCreate,doubleIndexValueCodes);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void doGImporter(String filePath,Set<String> noCreate,Set<String> doubleIndexValueCodes){

		ImportParams params = new ImportParams();
		params.setTitleRows(1);
		params.setHeadRows(2);
		params.setNeedSave(false);

		File file = new File(filePath);

		List<NewGIndexVo> results = ExcelImportUtil.importExcel(file, NewGIndexVo.class, params);
		log.info("results size=" + results.size());

		/**************************************************************************
		 * 处理权重  处理指标值
		 */
		try {
			this.processGIndexData(results,noCreate,doubleIndexValueCodes);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**************************************************************************
	 * 处理权重  处理指标值
	 */
    private void processCIndexData(List<NewCIndexVo> results,Set<String> noCreate,Set<String> doubleIndexValueCodes) throws Exception{
		log.info("unit,start index weight...");

		for (NewCIndexVo indexVo : results) {
			String indexCode = indexVo.getCode();
			List<KpiIndexValueVo> indexValues = indexVo.getIndexValues();
			Field[] field = indexVo.getClass().getDeclaredFields();
			for(int j=0 ; j<field.length ; j++){
				String fieldName = field[j].getName();
				String firstLetter = fieldName.substring(0, 1).toUpperCase();
				String getter = "get" + firstLetter + fieldName.substring(1);
				Method method = null;
				method = indexVo.getClass().getMethod(getter, new Class[] {});
				String orgCode = fieldName.substring(1);
				if(fieldName.startsWith("W")){
					String valueStr = (String)method.invoke(indexVo, new Object[] {});
					kpiImportUpdater.updateIndexWeight(KpiConsts.INDEX_GROUP_C, indexCode, orgCode, valueStr,noCreate);
				}else if(fieldName.startsWith("V")){
					List<KpiIndexDataVo> indexData = (List<KpiIndexDataVo>)method.invoke(indexVo, new Object[] {});
					kpiImportUpdater.updateIndexValueData(indexCode, orgCode, indexData, indexValues,doubleIndexValueCodes);
				}
			}
		}
		log.info("unit,end index weight.");
	}

	/**************************************************************************
	 * 处理权重  处理指标值
	 */
	private void processGIndexData(List<NewGIndexVo> results,Set<String> noCreate,Set<String> doubleIndexValueCodes) throws Exception{
		log.info("unit,start index weight...");
		for (NewGIndexVo indexVo : results) {
			String indexCode = indexVo.getCode();
			List<KpiIndexValueVo> indexValues = indexVo.getIndexValues();
			Field[] field = indexVo.getClass().getDeclaredFields();
			for(int j=0 ; j<field.length ; j++){
				String fieldName = field[j].getName();
				String firstLetter = fieldName.substring(0, 1).toUpperCase();
				String getter = "get" + firstLetter + fieldName.substring(1);
				Method method = null;
				method = indexVo.getClass().getMethod(getter, new Class[] {});
				String orgCode = fieldName.substring(1);
				if(fieldName.startsWith("W")){
					String valueStr = (String)method.invoke(indexVo, new Object[] {});
					kpiImportUpdater.updateIndexWeight(KpiConsts.INDEX_GROUP_G, indexCode, orgCode, valueStr,noCreate);
				}else if(fieldName.startsWith("V")){
					List<KpiIndexDataVo> indexData = (List<KpiIndexDataVo>)method.invoke(indexVo, new Object[] {});
					kpiImportUpdater.updateIndexValueData(indexCode, orgCode, indexData, indexValues,doubleIndexValueCodes);
				}
			}
		}
		log.info("unit,end index weight.");
	}
}
